package com.javasm.finance.dao.impl;

import com.javasm.finance.dao.CheckDao;
import com.javasm.finance.entity.Prod;
import com.javasm.finance.util.DBUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CheckDaoImpl implements CheckDao {
    QueryRunner runner=new QueryRunner();

    @Override
    public List<Prod> queryProd(Integer page, Integer pageSize, String prodName, String managerName, Integer prodState) throws SQLException {
        Connection conn = DBUtils.getConn();
        String sql = "select prod_id,prod_name,parent_class,creator_name,cre_time,mod_time,prod_state from fin_prod_basic ";
        //条件查询
        boolean isWhere=true;
        List paramList=new ArrayList();
        if (prodName != null && !"".equals(prodName)) {
            if (isWhere) {
                sql += "where ";
                isWhere = false;
            } else {
                sql += "and ";
            }
            sql += "prod_name like ? ";
            paramList.add("%" + prodName + "%");
        }
        if (managerName!=null&&!"".equals(managerName)){
            if (isWhere){
                sql+="where ";
                isWhere = false;
            }else {
                sql+="and ";
            }
            sql+="manager_name=? ";
            paramList.add(managerName);
        }
        if (prodState!=null&&!"".equals(prodState)){
            if (isWhere){
                sql+="where ";
            }else {
                sql+="and ";
            }
            sql+="prod_state=? ";
            paramList.add(prodState);
        }
        //分页查询
        sql += "limit " + (page - 1) * pageSize + "," + pageSize;
        BeanListHandler<Prod> listHandler = new BeanListHandler<>(Prod.class, new BasicRowProcessor(new GenerousBeanProcessor()));
        List<Prod> prodList = runner.query(conn, sql, listHandler, paramList.toArray());
        DBUtils.getClose(conn,null,null);
        return prodList;
    }

    @Override
    public long findTotalNum(String prodName, String managerName, Integer prodState) throws SQLException {
        Connection conn = DBUtils.getConn();
        String sql = "select count(1) totalNum from fin_prod_basic ";
        boolean isWhere=true;
        List paramList=new ArrayList();
        if (prodName != null && !"".equals(prodName)) {
            if (isWhere) {
                sql += "where ";
                isWhere = false;
            } else {
                sql += "and ";
            }
            sql += "prod_name like ? ";
            paramList.add("%" + prodName + "%");
        }
        if (managerName!=null&&!"".equals(managerName)){
            if (isWhere){
                sql+="where ";
                isWhere = false;
            }else {
                sql+="and ";
            }
            sql+="manager_name=? ";
            paramList.add(managerName);
        }
        if (prodState!=null&&!"".equals(prodState)){
            if (isWhere){
                sql+="where ";
            }else {
                sql+="and ";
            }
            sql+="prod_state=? ";
            paramList.add(prodState);
        }
        long totalNum = runner.query(conn, sql, new ScalarHandler<>(), paramList.toArray());
        DBUtils.getClose(conn, null, null);
        return totalNum;
    }

    @Override
    public int deleteProd(Integer prodId) throws SQLException {
        Connection conn = DBUtils.getConn();
        String sql = "delete from fin_prod_basic where prod_id=?";
        int rows = runner.update(conn, sql, prodId);
        DBUtils.getClose(conn, null, null);
        return rows;
    }

    @Override
    public Prod findByPid(Integer prodId) throws SQLException {
        Connection conn = DBUtils.getConn();
        String sql = "select prod_id,prod_name,prod_category_name,parent_class,prod_channel_name,ope_time,currency_type,anl_rate,subs_period,fund_manage_rate,subs_rate,ini_invest_amt,subs_co_method,red_cycle,red_start_amt,red_fee,lock_time,manager_name " +
                "from fin_prod_basic " +
                "where prod_id=?";
        BeanHandler<Prod> beanHandler = new BeanHandler<>(Prod.class, new BasicRowProcessor(new GenerousBeanProcessor()));
        Prod prod = runner.query(conn, sql, beanHandler, prodId);
        DBUtils.getClose(conn, null, null);
        return prod;
    }

    @Override
    public int edit(Integer prodId) throws SQLException {
        Connection conn = DBUtils.getConn();
        String sql = "update fin_prod_basic set prod_state=0 where prod_id=?;";
        int rows = runner.update(conn, sql,prodId);
        DBUtils.getClose(conn, null, null);
        return rows;
    }

    @Override
    public int failed(Integer prodId) throws SQLException {
        Connection conn = DBUtils.getConn();
        String sql = "update fin_prod_basic set prod_state=2 where prod_id=?;";
        int rows = runner.update(conn, sql,prodId);
        DBUtils.getClose(conn, null, null);
        return rows;
    }
}
